package com.joonhat.MonkeyFlash.hotkey;

import java.util.Vector;

import net.rim.device.api.system.ApplicationDescriptor;

public class KeyRecover {

	private static char newestHotkey;
	private static char discardedHotkey;
	
	//Rim에서 만든 앱은 description에 일반사용자가 만든것은 name에 언더바가 존재함
	public static void recoverHotkeyWithDescriptor(ApplicationDescriptor recoveryApp){
		if (HotkeyManager.isRimApp(recoveryApp)) {
			Hotkey.setIconName(recoveryApp.getDescription().toString(), recoveryApp);
		} else {
			Hotkey.setIconName(recoveryApp.getName(), recoveryApp);
		}
	}
	
	public static void recoverDiscardedHotkey(char newHotkey){
		updateNewestKeyAndDiscardedKey(newHotkey);
		if (isVoidHotkey(discardedHotkey)) 
			return;
		Vector discardedHotkeyMatchList = HotkeyManager
				.getDescriptorsFor(discardedHotkey);
		for (int i = 0; i < discardedHotkeyMatchList.size(); i++){
			System.out.println("	!"+((ApplicationDescriptor) discardedHotkeyMatchList
					.elementAt(i)).getName());
			recoverHotkeyWithDescriptor((ApplicationDescriptor) discardedHotkeyMatchList
					.elementAt(i));
		}
	}

	private static void updateNewestKeyAndDiscardedKey(char newestHotkey){
		KeyRecover.discardedHotkey = KeyRecover.newestHotkey;
		KeyRecover.newestHotkey = newestHotkey;
	}
	
	public static char getNewestHotkey() {
		return newestHotkey;
	}


	public static char getDiscardedHotkey() {
		return discardedHotkey;
	}
	
	private static boolean isVoidHotkey(char newHotkey) {
		return newHotkey == 0;
	}
	
}
